package com.ubisolv.vmakeup.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.util.Log;

public class ResourceUtil {

	public static List<Map<String, String>> getListResource(Context context, final int xmlId, final String item) {
		final List<Map<String, String>>	result =	new ArrayList<Map<String, String>>();
		
		Resources resources =	context.getResources();
		XmlResourceParser parser =	resources.getXml(xmlId);
		
		try {
			int type =	parser.getEventType();
			
			while (type != XmlPullParser.END_DOCUMENT) {
				if (type == XmlPullParser.START_TAG) {
					if (parser.getName().equals(item)) {
						HashMap<String, String> map =	new HashMap<String, String>();
						
						for (int i = 0; i < parser.getAttributeCount(); i++) {
							map.put(parser.getAttributeName(i), parser.getAttributeValue(i));
						}
						
						result.add(map);
					}
				}
				type =	parser.next();
			}
		} catch (IOException e) {
			Log.e("IOException", e.getStackTrace().toString());
		} catch (XmlPullParserException e) {
			Log.e("XmlPullParserException", e.getStackTrace().toString());
		} finally {
			parser.close();
		}
		
		return result;
	}
}
